package com.panicstyle.Moojigae;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import com.panicstyle.Moojigae.HttpRequest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class ArticleView extends Activity {
	/** Called when the activity is first created. */
	protected String itemsTitle;
	protected String itemsLink;
	protected HttpClient httpClient;
	protected HttpContext httpContext;
	
	String g_isPNotice;
	String g_isNotice;
	String g_Subject;
	String g_UserName;
	String g_UserID;
	String g_Date;
	String g_Link;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        
        MoojigaeApplication app = (MoojigaeApplication)getApplication();
        httpClient = app.httpClient;
        httpContext = app.httpContext;
		
        intenter();

/*        
        // Login
		Login login = new Login();
		
		if (!login.LoginTo(httpClient, httpContext, cookieStore)) {
			return;
		}
*/
		
		String htmlDoc = getData(httpClient, httpContext);
		
		WebView webview = new WebView(this);
		setContentView(webview);
		webview.loadDataWithBaseURL("http://121.134.211.159", htmlDoc, "text/html", "utf-8", "");
    }
    
    public void intenter() {
//    	Intent intent = getIntent();  // 값을 가져오는 인텐트 객체생성
    	Bundle extras = getIntent().getExtras();
    	// 가져온 값을 set해주는 부분
    	g_Subject = extras.getString("SUBJECT").toString();
    	g_UserName = extras.getString("USERNAME").toString();
    	g_Date = extras.getString("DATE").toString();
    	g_Link = extras.getString("LINK").toString();
    }
    
    protected String getData(HttpClient httpClient, HttpContext httpContext) {		
		
		String url = "http://121.134.211.159/" + g_Link;
		HttpRequest httpRequest = new HttpRequest();

        String result = httpRequest.requestGet(httpClient, httpContext, url, "", "euc-kr");
        
//        String strTitle = "<div align='left'>" + g_Subject + "</div><br /><div align='right'>" + g_UserName + " [" + g_Date + "]</div><hr>";
        
        Pattern p = Pattern.compile("(?<=<!-- 게시물 레코드 반복-->)(.|\\n)*?(?=<!-- 메모 입력 -->)", Pattern.CASE_INSENSITIVE); 
        Matcher m = p.matcher(result);
        
        String strContent;
        if (m.find()) { // Find each match in turn; String can't do this.     
        	strContent = m.group(0);
        } else {
        	strContent = "";
        }

        strContent = strContent.replaceAll("<td width=200 align=right class=fMemoSmallGray>", "<!--");
        strContent = strContent.replaceAll("<td width=10></td>", "-->");
        strContent = strContent.replaceAll("<!-- 메모에 대한 답변 -->", "<!--");
        strContent = strContent.replaceAll("<!-- <font class=fMemoSmallGray>", "--><!--");

//        String strHeader = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
//        strHeader += "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>";
//        String strBottom = "</body></html>";
        String cssStr = "<link href=\"./css/default.css\" rel=\"stylesheet\">";

    	String htmlDoc = cssStr + strContent + "</table>";

        return htmlDoc;
    }

}
